#include "led_effects.h"

int redPin = 12;
int greenPin = 11;
int bluePin = 10;

int speakerPin = 8;
int knobPin = 0;
int photoPin = 2;

int length = 56; // the number of notes
char notes[] = "gbbCbbggbbCbbggbbCbbggbbCbbcceeCeecceeCeecceeCeecceeCCCC"; // a space represents a rest
int beats[] = { 
  3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1 };
int tempo = 300;
int loopMax = 33;
int trigger = random(1, 7);
int lastTrigger = trigger;

float duration = 0.0;

int photoThreshold = 15;
int photoState = 0;

int at = 0;
int m_at = 0;
int i = 0;

char names[] = { 
  'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 
  1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

LED_Effects le(redPin, greenPin, bluePin);

void playTone(int tone, float duration)
{
  for (long i = 0; i < duration * 1000L; i += tone * 2)
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
  
}

void playNote(char note, float duration)
{
  
  // play the tone corresponding to the note name
  for (i = 0; i < 8; i++)
  {
    if (names[i] == note)
      playTone(tones[i], duration);
  }
  
}

void setup()
{
  pinMode(speakerPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{

  photoState = analogRead(photoPin);
  tempo = (analogRead(knobPin) / 4.096) + 20;
  
  Serial.println(tempo, DEC);
  
  if (photoState < photoThreshold)
  {

    // if photocell was just triggered, get new random function
    if (at >= loopMax || trigger == 0)
    {

      le._reset();

      // ensure different trigger each pass
      while (trigger == lastTrigger)
      {
        trigger = random(1, 6);
      }

      lastTrigger = trigger;
      at = 0;
      
    }

    if (m_at >= length)
    {
      m_at = 0;
    }

    switch (trigger)
    {

    case 1:
      Serial.println("Trigger 1: downStep");
      le.downStep();
      break;

    case 2:
      Serial.println("Trigger 2: downUp");
      le.downUp();
      break;

    case 3:
      Serial.println("Trigger 3: upStep");
      le.upStep();
      break;

    case 4:
      Serial.println("Trigger 4: allBlink");
      le.allBlink();
      break;

    case 5:
      Serial.println("Trigger 5: scroll");
      le.scroll();
      break;

    case 6:
      Serial.println("Trigger 6: bounce");
      le.bounce();
      break;

    case 7:
      Serial.println("Trigger 7: spring");
      le.spring();
      break;

    default:
      trigger = 0;
      break;

    }
    
    duration = (float)beats[m_at] * (float)tempo;
    
    Serial.println(duration, DEC);
    
    if (notes[m_at] == ' ')
    {
      delay(duration); // rest
    }
    else
    {
      playNote(notes[m_at], duration);
    }

    delay(tempo / 2);
    at++;
    m_at++;

  }
  else
  {
    // lights came on, reset everything
    le._reset();
    trigger = 0;
    at = 0;
    m_at = 0;
  }

}


